
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Swaf;
using Swaf.BizObj;
namespace GoogleAnalytics.Gui.AspNet
{
	[ToolboxData("<{0}:AnalyticsCodeGenerator runat=server></{0}:AnalyticsCodeGenerator>")]
	public class AnalyticsCodeGenerator : WebControl
	{
		protected override void Render(HtmlTextWriter writer)
		{
			if (!Page.Request.Url.Host.StartsWith("localhost", StringComparison.CurrentCultureIgnoreCase))
			{
				IApplication app = Swaf.Application.currentApp;
				object cid = app.securityMgr.currentPrincipal.additionalInfo.get("companyId");
				IBizObj company = app.ruleMgr.execute("getCachedCompanyInfo", cid).CallResults as IBizObj;
				if (company != null && company.containsField("settings.analyticsCode") && company.get("settings.analyticsCode").ToString().Length > 0)
				{
					string code = company.get("settings.analyticsCode").ToString();
					writer.Write(string.Format(s_script, code));
				}
			}
		}
		protected static string s_script = @"
<script type=""text/javascript"">
var gaJsHost = ((""https:"" == document.location.protocol) ? ""https://ssl."" : ""http://www."");
document.write(unescape(""%3Cscript src='"" + gaJsHost + ""google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E""));
</script>
<script type=""text/javascript"">
var pageTracker = _gat._getTracker(""{0}"");
pageTracker._initData();
pageTracker._trackPageview();
</script>";
	}
}
